package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.api.dto.ItemDTO;
import com.hmall.api.dto.OrderDetailDTO;
import com.hmall.common.exception.BizIllegalException;
import com.hmall.common.utils.BeanUtils;

import com.hmall.item.domain.po.Item;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.service.IItemService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author 虎哥
 */
@Service
@RequiredArgsConstructor
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements IItemService {

    private final ItemMapper itemMapper;
    @Override
    @Transactional // 加入事务，出错时可以回滚;
    public void deductStock(List<OrderDetailDTO> items) {
        System.out.println("deductStock items = " + items);
//        String sqlStatement = "com.hmall.mapper.item.ItemMapper.updateStock";
        boolean r = false;
        try {
//            r = executeBatch(items, (sqlSession, entity) -> sqlSession.update(sqlStatement, entity));
            for (OrderDetailDTO item : items) {
                itemMapper.updateStock(item);
            }
        } catch (Exception e) {
            throw new BizIllegalException("更新库存异常，可能是库存不足!", e);
        }
        /*if (!r) {
            throw new BizIllegalException("库存不足！");
        }*/
    }

    @Override
    public List<ItemDTO> queryItemByIds(Collection<Long> ids) {
        return BeanUtils.copyList(listByIds(ids), ItemDTO.class);
    }
}
